#ifdef KINECT

#include "inc\GUI\kinect\kinectstreamrecorderdialog.h"
#include "ui_kinectstreamrecorderdialog.h"

#include "Kinect/kinectimage.h"
#include "Core/Robot/nullbot.h"

#include "GUI/Troy/troyframe.h"

KinectStreamRecorderDialog::KinectStreamRecorderDialog(kinect::Kinect* kinect, QWidget *parent) :
    KinectDialog(kinect, parent),
    ui(new Ui::KinectStreamRecorderDialog),
    _currentAction(NULL),
    _returnAction(NULL)
{
    ui->setupUi(this);

    ui->okButton->setDisabled(true);
    ui->stopButton->setDisabled(true);

    connect(ui->okButton, SIGNAL(clicked()), this, SLOT(accept()));
    connect(ui->cancelButton, SIGNAL(clicked()), this, SLOT(reject()));

    connect(ui->startButton, SIGNAL(clicked()), this, SLOT(start()));
    connect(ui->stopButton, SIGNAL(clicked()), this, SLOT(stop()));
    \
    kinect::KinectImage* image = getKinect()->addComponent<kinect::KinectImage>();
    connect(image, SIGNAL(depthMapChanged(QImage*)), this, SLOT(updateImage(QImage*)));

    ui->troy->mirror(true);

    maximizeSize();
}

KinectStreamRecorderDialog::~KinectStreamRecorderDialog()
{
    delete ui;
    if (_returnAction != NULL) {
        if (_returnAction->parent() == this && _returnAction->getRobot()->isNull()) {
            _returnAction->deleteLater();
        }
    }
}

#ifdef WIIMOTE
void KinectStreamRecorderDialog::associateWiimote(WiimoteStatusDialog *dialog) {
    if (hasWiimoteDialog()) {
        getWiimoteDialog()->setCommand(qWiimote::A, "Start/Stop Recording", this, SLOT(toggle()));
        getWiimoteDialog()->setCommand(qWiimote::Minus, "Cancel", this, SLOT(reject()));
        getWiimoteDialog()->setCommand(qWiimote::B, "Cancel", this, SLOT(reject()));
        getWiimoteDialog()->setCommand(qWiimote::Home, "Reset", getKinectSkeleton(), SLOT(reset()));
    }
}
#endif

RobotAction* KinectStreamRecorderDialog::getRecordedAction() {
    if (_returnAction != NULL) {
        _returnAction->setParent(NULL);
    }
    return _returnAction;
}

void KinectStreamRecorderDialog::start() {
    ui->startButton->setDisabled(true);
    ui->stopButton->setDisabled(false);

    _startTime.start();
    _currentAction = new RobotAction(new NullBot(), "New Recorded Action", this);
}

void KinectStreamRecorderDialog::stop() {
    ui->startButton->setDisabled(false);
    ui->stopButton->setDisabled(true);

    ui->okButton->setDisabled(false);
    _returnAction = _currentAction;
#ifdef WIIMOTE
    if (hasWiimoteDialog()) {
        getWiimoteDialog()->setCommand(qWiimote::Plus, "OK", this, SLOT(accept()));
    }
#endif
    _currentAction = NULL;
}

void KinectStreamRecorderDialog::toggle() {
    if (_currentAction == NULL) {
        start();
    } else {
        stop();
    }
}

void KinectStreamRecorderDialog::updateImage(QImage* image) {
    ui->kinectCamera->setPixmap(QPixmap::fromImage(*image));
}

/*
void KinectStreamRecorderDialog::kinectKeyFrame(MotionKeyFrame keyFrame) {
    ui->troy->loadKeyFrame(keyFrame);
    if (_currentAction != NULL) {
        keyFrame.timestamp = _startTime.elapsed();
        if (keyFrame.timestamp >= _currentAction->motionDuration()) {
            _currentAction->setMotionDuration(keyFrame.timestamp + 10);
        }
        _currentAction->addKeyFrame(keyFrame);
    }
}
*/

void KinectStreamRecorderDialog::receiveTroyPose(QSharedPointer<TroyPose> pose) {
    foreach (int joint, pose->joints()) {
        ui->troy->setJointAngle(joint, pose->jointAngle(joint));
    }
}

#endif
